package com.example.huitong.ui;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.huitong.App;
import com.example.huitong.R;
import com.example.huitong.fragments.QueryFragment;
import com.example.huitong.fragments.TestFragment;

public class MainHomeActivity extends FragmentActivity implements OnTabChangeListener {
	private FragmentTabHost mTabHost;
	TabWidget mTabWidget;
	private static final String TAG_QUERY = "tag_Query";
	private static final String TAG_SNED = "tag_Send";
	private static final String TAG_MY_EXPRESS = "tag_Express";
	private static final String TAG_SERVICE = "tag_Service";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
//		mTabWidget.setBackgroundResource(R.drawable.bg_nav);
		mTabWidget.setDividerDrawable(null);
		
		getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
		
		mTabHost.addTab(mTabHost.newTabSpec(TAG_QUERY).setIndicator(
				getIndicatorView(R.drawable.tab_grab, R.string.query)), QueryFragment.class, null);
		
		mTabHost.addTab(mTabHost.newTabSpec(TAG_SNED).setIndicator(
				getIndicatorView(R.drawable.tab_buy, R.string.sent)), TestFragment.class, null);
		
		mTabHost.addTab(mTabHost.newTabSpec(TAG_MY_EXPRESS).setIndicator(
				getIndicatorView(R.drawable.tab_manage, R.string.my_express)), TestFragment.class, null);
		
		mTabHost.addTab(mTabHost.newTabSpec(TAG_SERVICE).setIndicator(
				getIndicatorView(R.drawable.tab_more, R.string.service)), TestFragment.class, null);
		
		mTabHost.setOnTabChangedListener(this);
		onTabChanged(TAG_QUERY);
	}

	private View getIndicatorView(int drawableResId, int labelResId){
		LayoutInflater inflater = getLayoutInflater();
		TextView view = (TextView) inflater.inflate(R.layout.tab_widget, null);
		view.setText(labelResId);
		view.setCompoundDrawablesWithIntrinsicBounds(0, drawableResId, 0, 0);
		return view;
	}

	@Override
	public void onTabChanged(String tab) {
		// TODO Auto-generated method stub
		System.out.println("tab --》"+tab);
		if(TAG_QUERY.equals(tab)){
			setTitle(R.string.query);
			
		}else if(TAG_SNED.equals(tab)){
			setTitle(R.string.sent);
			
		}else if(TAG_MY_EXPRESS.equals(tab)){
			setTitle(R.string.my_express);
			
		}else if(TAG_SERVICE.equals(tab)){
			setTitle(R.string.service);
		}
	}
	
	private long mLastExitTime = -1;
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		long now = System.currentTimeMillis();
		if ((now - mLastExitTime) < 1000) {
			super.onBackPressed();
		} else {
			App.toastShort(this, "再按一次退出");
			mLastExitTime = now;
		}

	}
}
